package com.yanqu.road.server.cmd.activity.cat.test;

import com.yanqu.road.entity.activity.cat.Position;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CatProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.cat.CatModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.cat.CatMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_CAT_CONNECTION_TEST, desc = "连线（练习模式）")
public class CatTestGameConnectCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        if (!ActivityMgr.activityInTime(CatMgr.getActivityInfo()) || CatMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        CatProto.CatTestGameConnectReqMsg reqMsg = CatProto.CatTestGameConnectReqMsg.parseFrom(packet.getBytes());
        List<Position> positionList = new ArrayList<>();
        if(reqMsg.getPointCount() == 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        for (CatProto.CatConnectPointTemp pointTemp : reqMsg.getPointList()) {
            positionList.add(new Position(pointTemp.getX(), pointTemp.getY()));
        }
        ret = player.getModule(CatModule.class).testConnect(positionList);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            CatProto.CatTestGameConnectRespMsg.Builder respMsg = CatProto.CatTestGameConnectRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CAT_CONNECTION_TEST, respMsg);
        }
    }
}
